﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class scenechangeManager : MonoBehaviour {
	
	public DialoguerDialogues diaDia;
	public GameObject actor;
	public GameObject bg;
	
	public bool isPressed;
	bool actorEntered;

	public List<GameObject> allActors;

	public List<GameObject> hiddenObj = new List<GameObject>();
	public MusicManager musicManager;
	public ChoiceInventoryHandler choiceInventoryHandler;
	
	void Awake()
	{
		Dialoguer.Initialize ();
		musicManager = GameObject.Find("MenuManager").GetComponent<MusicManager>();

	}
	// Use this for initialization
	void Start () {
		Dialoguer.events.onMessageEvent += HandleonMessageEvent;
		allActors = new List<GameObject>();
		choiceInventoryHandler = GameObject.Find("InventoryManager").GetComponent<ChoiceInventoryHandler>();
		hiddenObj = new List<GameObject>(GameObject.FindGameObjectsWithTag("Hidden").OrderBy(x => x.name));
		foreach (GameObject go in hiddenObj)
			go.SetActive(false);
	}
	
	void HandleonMessageEvent (string message, string metadata)
	{
		/*if(message == "SetAnimation")
		{
			string[] strings = metadata.Split(new char[]{','});

			allActors.Find(x => x.name == strings[0]).GetComponent<AnimationManager>().SetAnimation(strings[1]);
		}
		else
			gameObject.SendMessage(message, metadata);*/

		switch(message)
		{
			case "SetAnimation":

			break;
			case "BackgroundChange":
			BackgroundChange(metadata);
			break;
			case "ActorEnter":
			print ("received: "+metadata);
			ActorEnter(metadata);
			break;
			case "ActorExit":
			ActorExit();
			break;
			case "PlayDialogue":
			PlayDialogue(metadata);
			break;
			case "ReplaceObjects":
			ReplaceObjects(metadata);
			break;
			case "BGExit":
			BGExit();
			break;
			case "loadFirstPuzzle":
			loadFirstPuzzle();
			break;
			case "SceneChange":
			SceneChange(metadata);
			break;
			case "SecondIntroduction":
			SecondIntroduction();
			break;
			case "PlayBGM":
			PlayBGM(metadata);
			break;
			case "PlaySFX":
			PlaySFX(metadata);
			break;
			case "UpdateProfile":
			UpdateProfile(metadata);
			break;
			case "ActivateChoose":
			ActivateChoose();
			break;
			case "AddItem":
			AddItem(metadata);
			break;
			case "onCameraShake":
			onCameraShake();
			break;
			default:
			return;
		}
	}

	void SetAnimation(string meta)
	{
		string[] strings = meta.Split(new char[]{','});			
		allActors.Find(x => x.name == strings[0]).GetComponent<AnimationManager>().SetAnimation(strings[1]);
	}
	
	void BackgroundChange(string meta)
	{
		bg = Resources.Load ("BG/"+meta, typeof(GameObject)) as GameObject;
		//print ("passed");
		Debug.Log ("Loading " + bg.name);
		Instantiate (bg);
	}

	void ActorEnter(string meta)
	{
		for(int i = 0; i<allActors.Count; i++)
			if(allActors[i].name == meta)
			{
				allActors[i].SetActive(true);
				return;
			}

		GameObject theActor = Resources.Load("ACTORS/Characters/" + meta, typeof(GameObject)) as GameObject;
		GameObject theClone = Instantiate(theActor) as GameObject;
		theClone.name = meta;
		theClone.transform.position = new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y, 0.0f);
		print ("Actor Enter: "+theClone);
		allActors.Add(theClone);
//		actor.SetActive(true);
	}

	void ActorExit()
	{
		for(int i = 0; i < allActors.Count; i++)
		{
			allActors[i].SetActive(false);
		}
	}

	public void PlayDialogue(string meta)
	{
		int i = int.Parse (meta);
		Dialoguer.StartDialogue (i);
		print (i.ToString ());
		PlayerPrefs.SetInt ("LastDia", i);
	}

	void ReplaceObjects(string meta)
	{
		//--obj to destroy,obj to replace (must be included in the list) 
		string[] strings = meta.Split (new char[]{','});
		Destroy (GameObject.Find (strings [0]));
		hiddenObj.Find (x => x.name == strings [1]).SetActive (true);
	}

	void BGExit()
	{
		GameObject exitBG = GameObject.Find ("BG03(Clone)");
		print (exitBG.name);
		exitBG.SetActive (false);
	}

	void loadFirstPuzzle()
	{
		Application.LoadLevel ("PuzzleRoom");
	}

	void SceneChange(string meta)
	{
		Application.LoadLevel (meta);
	}

	void SecondIntroduction()
	{
		diaDia = DialoguerDialogues.Intro_LazarusProtocol;
		Dialoguer.StartDialogue(diaDia, DialoguerCallback);
	}

	void PlayBGM(string meta)
	{
		musicManager.PlayBGM(Resources.Load("BGM/" + meta, typeof(AudioClip)) as AudioClip);
	}

	void PlaySFX(string meta)
	{
		musicManager.PlaySoundEffect(Resources.Load("SFX/" + meta, typeof(AudioClip)) as AudioClip);
	}

	void onCameraShake()
	{
		//Camera.main.GetComponent<camerashake> ().onCameraShake ();
		print("shaking");
	}
	
	void UpdateProfile(string meta)
	{
		string[] strings = meta.Split(new char[]{','});
		for(int i = 0; i < GameObject.Find("InventoryManager").GetComponent<SampleInventoryHandler>().profiles.Count; i++)
			if(GameObject.Find("InventoryManager").GetComponent<SampleInventoryHandler>().profiles[i].name == strings[0])
			{
				Profiles.UpdateDescription(GameObject.Find("InventoryManager").GetComponent<SampleInventoryHandler>().profiles, strings[0], strings[1]);
				return;
			}

		GameObject.Find("InventoryManager").GetComponent<SampleInventoryHandler>().profiles.Add(Profiles.AddProfile(strings[0]));
		GameObject.Find("InventoryManager").GetComponent<SampleInventoryHandler>().profiles.Find(x => x.name == strings[0]).description = strings[1];
	}

	void ActivateChoose()
	{
		MenuView mv = GameObject.Find("MenuDisplay").GetComponent<MenuView>();
		mv.onActiveChoiceInventory();
	}

	void MainMenuLoad()
	{
		Application.LoadLevel ("MainMenu");
	}

	void AddItem(string meta)
	{
		SampleInventoryHandler sih = GameObject.Find("InventoryManager").GetComponent<SampleInventoryHandler>();
		for(int i = 0; i < sih.playerInventory.Count; i++)
			if(sih.playerInventory[i].Name == meta)
				return;

		sih.playerInventory.Add(ItemHelper.GetItemByName(meta));
	}
	
	// Update is called once per frame
	void Update () {

	}

	void OnLevelWasLoaded(int num){
		allActors = new List<GameObject>();
		musicManager = GameObject.Find("MenuManager").GetComponent<MusicManager>();
		choiceInventoryHandler = GameObject.Find("InventoryManager").GetComponent<ChoiceInventoryHandler>();
		hiddenObj = new List<GameObject>(GameObject.FindGameObjectsWithTag("Hidden").OrderBy(x => x.name));
		foreach (GameObject go in hiddenObj)
			go.SetActive(false);
		hiddenObj = new List<GameObject>(GameObject.FindGameObjectsWithTag("Hidden"));
		for(int i = 0; i < hiddenObj.Count; i++)
			hiddenObj[i].SetActive(false);
		switch(num)
		{
			case 2:
			Debug.Log("called!" + num);
			Dialoguer.StartDialogue(DialoguerDialogues.PreIntroduction);
			PlayBGM("deat_miho");
			break;
			case 3:
			Debug.Log("called!" + num);
			Dialoguer.StartDialogue(DialoguerDialogues.First_Puzzle_Items);
			break;
			case 5:
			Debug.Log("called!" + num);
			Dialoguer.StartDialogue(DialoguerDialogues.NIGHT_2);
			AddItem("Miho's Memento");
			AddItem("Sanctus Bio-chip");
			AddItem("Tanto Blade");
			break;
			case 6:
			Debug.Log("called!" + num);
			Dialoguer.StartDialogue(DialoguerDialogues.NIGHT3);
			AddItem("Miho's Memento");
			AddItem("Sanctus Bio-chip");
			AddItem("Tanto Blade");
			break;
			case 7:
			Debug.Log("called!" + num);
			Dialoguer.StartDialogue(DialoguerDialogues.NIGHT_4);
			AddItem("Miho's Memento");
			AddItem("Sanctus Bio-chip");
			AddItem("Tanto Blade");
			break;
			default:
			Debug.Log("called!" + num);
			break;
		}
	}
	
	private void DialoguerCallback()
	{
		this.enabled = true;
	}
}
